/**
 * 
 */
package com.dream.house.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.servyou.utils.CoreUtils;
import cn.com.servyou.utils.Page;

import com.dream.house.entity.AllowanceDTO;
import com.dream.house.entity.GroupDTO;
import com.dream.house.entity.UserDTO;
import com.dream.house.service.AllowanceService;
import com.dream.house.service.UserService;
import com.dream.house.utils.Constants;

/**
 * @author maoq
 *
 */
@Controller
@RequestMapping("allowance")
public class AllowanceController {
	
	@Autowired
	AllowanceService allowanceService;
	@Autowired
	UserService userService;
	
	@RequestMapping("index")
	public String index() {
		return "allowance/index";
	}
	
	@RequestMapping("gotobutiechaxun")
	public String gotobutiechaxun() {
		return "allowance/butie_chaxun";
	}
	
	@RequestMapping("butiechaxun")
	public String butiechaxun(@ModelAttribute("dto") AllowanceDTO dto, Model model, int pageNo, int pageSize, String flag) {
		if ("shenpi".equals(flag)) {
			dto.setCheck_status(Constants.CHECK_STATUS_INIT);
		}
		Map<String, Object> condition = CoreUtils.entity2Map(dto);
		Page page = allowanceService.findByPage(condition, pageNo, pageSize);
		model.addAttribute("page", page);

		if ("shenpi".equals(flag)) {
			return "shenpi/butie_list";
		}
		
		return "allowance/butie_list";
	}
	
	@RequestMapping("gotodengji")
	public String gotodengji(Model model, HttpSession session) {
		UserDTO user = (UserDTO)session.getAttribute(Constants.CURRENT_USER);
		List<GroupDTO> groupList = userService.getGroups(user.getUserId());
		model.addAttribute("groupList", groupList);
		return "allowance/butie_dengji";
	}
	
	@RequestMapping("dengji")
	public String dengji(AllowanceDTO dto, Model model, HttpSession session) {
		UserDTO user = (UserDTO)session.getAttribute(Constants.CURRENT_USER);
		dto.setCreate_user(user.getUserId());
		try {
			allowanceService.saveAllowance(dto);
			model.addAttribute("error","1");
		} catch (Exception e) {
			model.addAttribute("error", "0");
		}
		return "allowance/butie_dengji";
	}
	
	@RequestMapping("edit/{id}")
	public String edit(@PathVariable String id, Model model, HttpSession session) {
		UserDTO user = (UserDTO)session.getAttribute(Constants.CURRENT_USER);
		List<GroupDTO> groupList = userService.getGroups(user.getUserId());
		model.addAttribute("groupList", groupList);
		AllowanceDTO allowance = allowanceService.getAllowance(id);
		model.addAttribute("allowance", allowance);
		return "allowance/butie_edit";
	}
	
	@RequestMapping("doedit")
	public String doEdit(AllowanceDTO dto, Model model, HttpSession session) {
		UserDTO user = (UserDTO)session.getAttribute(Constants.CURRENT_USER);
		dto.setModify_user(user.getUserId());
		try {
			allowanceService.modifyAllowance(dto);
			model.addAttribute("error", "1");
		} catch (Exception e) {
			model.addAttribute("error", "0");
		}
		AllowanceDTO allowance = allowanceService.getAllowance(dto.getAllowance_id());
		model.addAttribute("allowance", allowance);
		List<GroupDTO> groupList = userService.getGroups(user.getUserId());
		model.addAttribute("groupList", groupList);
		return "allowance/butie_edit";
	}
	
	@ResponseBody
	@RequestMapping("delete/{id}")
	public String delete(@PathVariable String id) {
		try {
			allowanceService.removeAllowance(id);
		} catch (Exception e) {
			return "0";
		}
		return "1";
	}
	
}
